/**
 * This file is part of AppStoreBot.
 *
 *  AppStoreBot is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AppStoreBot is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AppStoreBot.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package lu.pragmaconsult.appstorebot.itunes;

import static lu.pragmaconsult.appstorebot.itunes.Constants.AJAX_NAME;
import static lu.pragmaconsult.appstorebot.itunes.Constants.DAILY_NAME;
import static lu.pragmaconsult.appstorebot.itunes.Constants.DATE_LIST_AVAILABLE_DAYS;
import static lu.pragmaconsult.appstorebot.itunes.Constants.DATE_LIST_AVAILABLE_WEEKS;
import static lu.pragmaconsult.appstorebot.itunes.Constants.DATE_NAME;
import static lu.pragmaconsult.appstorebot.itunes.Constants.SALES_URL;
import static lu.pragmaconsult.appstorebot.itunes.Constants.SELECT_NAME;
import static lu.pragmaconsult.appstorebot.itunes.Constants.VIEW_STATE;
import lu.pragmaconsult.appstorebot.BotContext;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;

/**
 * TODO: Type comment
 * @author Wilfried Vandenberghe (wilfried.vandenberghe at pragmaconsult.lu)
 */
class SalesReportPageBotState extends AbstractBotState {
	
	@Override
	protected HttpMethod createHttpMethod(BotContext context, HttpClient httpClient) {
		return new GetMethod(SALES_URL);
	}

	@Override
	protected BotState getNextState() {
		return new FormSalesReportBotState();
	}

	@Override
	protected void processResponseBody(BotContext context, HttpMethod method) {
		try {
			HtmlParser parser = new HtmlParser(method.getResponseBodyAsStream());
			
			// get the view state value
			String state = parser.getState();
			context.setContextAttribute(VIEW_STATE, state);
			
			// get post data
			String id = parser.getScriptId();
			context.setContextAttribute(DAILY_NAME, id);
			context.setContextAttribute(AJAX_NAME, id.substring(0, id.length() - 2) + "_2");
			if(context.isWeekly()) {
				context.setContextAttribute(DATE_NAME, id.substring(0, id.length() - 2) + "_7");
				context.setContextAttribute(SELECT_NAME, id.substring(0, id.length() - 2) + "_37");
			} else {
				context.setContextAttribute(DATE_NAME, id.substring(0, id.length() - 2) + "_8");
				context.setContextAttribute(SELECT_NAME, id.substring(0, id.length() - 2) + "_32");
			}
			
			// get daily date and weekly date
			StringBuilder dayBuilder = new StringBuilder();
			for(String dayDate : parser.getDayOptionValues()) {
				if(dayBuilder.length() != 0) {
					dayBuilder.append(",");
				}
				dayBuilder.append(dayDate);
			}
			context.setContextAttribute(DATE_LIST_AVAILABLE_DAYS, dayBuilder.toString());
			
			StringBuilder weekBuilder = new StringBuilder();
			for(String weekDate : parser.getWeekOptionValues()) {
				if(weekBuilder.length() != 0) {
					weekBuilder.append(",");
				}
				weekBuilder.append(weekDate);
			}
			context.setContextAttribute(DATE_LIST_AVAILABLE_WEEKS, weekBuilder.toString());
		} catch(Exception e) {
			throw new IllegalStateException(e);
		}
	}
	
}
